package com.wmm.jhdriver;

import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputEditText;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.common.dialog.MyDialog;
import com.common.net.mywidget.LoadingDialog;
import com.common.util.SPUtils;
import com.google.gson.Gson;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import cz.msebera.android.httpclient.Header;

/**
 * 登录页面
 * @author wmm
 * 2018-12-03 18:02
 */
public class LoginActivity extends AppCompatActivity {
    private TextInputEditText etOne,etTwo,etThree;
    private LoadingDialog dialog;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        etOne=findViewById(R.id.et_yu);
        etTwo=findViewById(R.id.et_count);
        etThree=findViewById(R.id.et_pwd);
        dialog=new LoadingDialog(this);
        dialog.setTitle("登录中...");
        findViewById(R.id.btn_cancle).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        findViewById(R.id.btn_login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(TextUtils.isEmpty(etOne.getText().toString())||TextUtils.isEmpty(etTwo.getText().toString())||TextUtils.isEmpty(etThree.getText().toString())){
                    T.showShort(LoginActivity.this,"登录信息不完整");
                    return;
                }
                doLogin();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        etOne.setText(SPUtils.get(this,"YuInfo","").toString());
        etThree.setText(SPUtils.get(this,"UserPwd","").toString());
        etTwo.setText(SPUtils.get(this,"UserName","").toString());
    }

    private void doLogin(){
        RequestParams requestParams=new RequestParams();
        Map<String,String> map=new HashMap<>();
        map.put("UserName","'"+etTwo.getText().toString().trim()+"'");
        map.put("UserPwd","'"+etThree.getText().toString().trim()+"'");
        map.put("YuInfo","'"+etOne.getText().toString().trim()+"'");
        requestParams.put("sJson",map.toString().replace("=",":"));
        HttpUtils.post(Constant.LOGINURL, requestParams, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
            }

            @Override
            public void onStart() {
                super.onStart();
                dialog.show();
            }

            @Override
            public void onFinish() {
                super.onFinish();
                dialog.dismiss();
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                try {
                    JSONObject object=new JSONObject(responseString);
                    if(object.getInt("success")==0){
                        SPUtils.put(LoginActivity.this,"UserName",etTwo.getText().toString().trim());
                        SPUtils.put(LoginActivity.this,"UserPwd",etThree.getText().toString().trim());
                        SPUtils.put(LoginActivity.this,"YuInfo",etOne.getText().toString().trim());
                        T.showShort(LoginActivity.this,"登录成功");
                        startActivity(new Intent(LoginActivity.this,MainActivity.class));
                        finish();
                    }else{
                        T.showShort(LoginActivity.this,object.getString("message"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
